<template>
  <div class="lottery-history">
    <h3>{{ name }}历史开奖记录</h3>

    <div v-if="loading" class="loading">加载中...</div>

    <div v-if="error" class="error">{{ error }}</div>

    <div v-if="total === 0 && !loading" class="no-data">暂无历史记录</div>
    <div v-if="total > 0 && !loading">
      <table class="history-table">
        <thead>
          <tr>
            <th>期号</th>
            <th>前区号码</th>
            <!-- 只有当hasBack为true时才显示后区号码列 -->
            <th v-if="hasBack">后区号码</th>
            <th>一等奖个数</th>
            <th>二等奖个数</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="item in historyData" :key="item.drawNumber">
            <td>{{ item.drawNumber }}</td>
            <td>
              <span v-for="num in item.front" :key="num" class="number ball-front">{{ num }}</span>
            </td>
            <!-- 只有当hasBack为true时才显示后区号码单元格 -->
            <td v-if="hasBack">
              <span v-for="num in item.back || []" :key="num" class="number ball-back">{{ num }}</span>
            </td>
            <td>{{ item.firstPrizeCount || 0 }}</td>
            <td>{{ item.secondPrizeCount || 0 }}</td>
          </tr>
        </tbody>
      </table>

      <div class="pagination">
        <p>
          共 {{ total }} 期，第 {{ currentPage }}/{{ totalPages }} 页
        </p>
        <div class="pagination-buttons">
          <button @click="changePage(currentPage - 1)" :disabled="currentPage <= 1">
            上一页
          </button>
          <button @click="changePage(currentPage + 1)" :disabled="currentPage >= totalPages">
            下一页
          </button>
          <div class="page-jump">
            <input type="number" v-model="pageInput" :min="1" :max="totalPages" @keyup.enter="jumpToPage">
            <button @click="jumpToPage">跳转</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, onMounted, defineProps } from 'vue'
import { getHistory } from '../api/api'

const props = defineProps({
  type: {
    type: String,
    required: true
  },
  name: {
    type: String,
    required: true
  },
  hasBack: {
    type: Boolean,
    default: true
  }
})

// 状态管理
const historyData = ref([])
const total = ref(0)
const currentPage = ref(1)
const totalPages = ref(0)
const loading = ref(false)
const error = ref('')
const pageInput = ref(1)

// 获取历史记录
const fetchHistory = async (page = 1) => {
  loading.value = true
  error.value = ''

  try {
    const result = await getHistory(props.type, page)

    if (result && result.code === 20000) {
      historyData.value = Array.isArray(result.data) ? result.data : []
      total.value = result.total || 0
      currentPage.value = page
      totalPages.value = Math.ceil(total.value / 20)
      pageInput.value = page
    } else {
      error.value = '获取历史记录失败'
    }
  } catch (err) {
    error.value = '网络错误，请稍后重试'
    console.error(err)
  } finally {
    loading.value = false
  }
}

// 页面变更
const changePage = (page) => {
  if (page >= 1 && page <= totalPages.value) {
    fetchHistory(page)
  }
}

// 跳转到指定页
const jumpToPage = () => {
  const page = parseInt(pageInput.value)
  if (!isNaN(page) && page >= 1 && page <= totalPages.value) {
    fetchHistory(page)
  }
}

// 监听类型变化
watch(() => props.type, () => {
  fetchHistory(1)
})

// 初始化
onMounted(() => {
  fetchHistory(1)
})
</script>

<style scoped>
.lottery-history {
  overflow-x: auto;
}

.history-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 1rem;
}

.history-table th,
.history-table td {
  padding: 0.8rem;
  text-align: left;
  border-bottom: 1px solid #eee;
}

.history-table th {
  background-color: #f5f5f5;
  font-weight: bold;
}

.number {
  display: inline-block;
  width: 30px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  border-radius: 50%;
  color: white;
  font-size: 0.8rem;
  margin-right: 5px;
}

.ball-front {
  background-color: #42b983;
}

.ball-back {
  background-color: #3498db;
}

.pagination {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 1rem;
  flex-wrap: wrap;
  gap: 1rem;
}

.pagination-buttons {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.pagination button {
  padding: 0.3rem 0.8rem;
  border: 1px solid #ddd;
  background-color: white;
  border-radius: 4px;
  cursor: pointer;
}

.pagination button:disabled {
  background-color: #f5f5f5;
  cursor: not-allowed;
  color: #999;
}

.page-jump {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.page-jump input {
  width: 50px;
  padding: 0.3rem;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.loading,
.no-data,
.error {
  text-align: center;
  padding: 2rem;
  color: #666;
}

.error {
  color: #ff4444;
}
</style>
